/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.serverpackets;

import static l1j.server.server.model.skill.L1SkillId.STATUS_THIRD_SPEED;

import l1j.server.server.Opcodes;
import l1j.server.server.datatables.DailyTable;
import l1j.server.server.model.Instance.L1PcInstance;

// Referenced classes of package l1j.server.server.serverpackets:
// ServerBasePacket, S_OtherCharPacks

public class S_OtherCharPacks extends ServerBasePacket {

	private static final String S_OTHER_CHAR_PACKS = "[S] S_OtherCharPacks";

	private static final int STATUS_POISON = 1;

	private static final int STATUS_INVISIBLE = 2;

	private static final int STATUS_PC = 4;

	private byte[] _byte = null;

	public S_OtherCharPacks(L1PcInstance pc, boolean isFindInvis) {
		buildPacket(pc, isFindInvis);
	}

	public S_OtherCharPacks(L1PcInstance pc) {
		buildPacket(pc, false);
	}

	private void buildPacket(L1PcInstance pc, boolean isFindInvis) {
		int status = STATUS_PC;

		if (pc.getPoison() != null) { // 毒状態
			if (pc.getPoison().getEffectId() == 1) {
				status |= STATUS_POISON;
			}
		}
		if (pc.isInvisble() && !isFindInvis) {
			status |= STATUS_INVISIBLE;
		}
		if (pc.getBraveSpeed() != 0) { // 2段加速效果
			status |= pc.getBraveSpeed() * 16;
		}

		// int addbyte = 0;
		// int addbyte1 = 1;

		writeC(Opcodes.S_OPCODE_CHARPACK);
		writeH(pc.getX());
		writeH(pc.getY());
		writeD(pc.getId());
		if (pc.isDead()) {
			writeH(pc.getTempCharGfxAtDead());
		}
		else {
			writeH(pc.getTempCharGfx());
		}
		if (pc.isDead()) {
			writeC(pc.getStatus());
		}
		else {
			writeC(pc.getCurrentWeapon());
		}
		writeC(pc.getHeading());
		// writeC(0); // makes char invis (0x01), cannot move. spells display
		writeC(pc.getChaLightSize());
		writeC(pc.getMoveSpeed());
		writeD(0x0000); // exp
		// writeC(0x00);
		writeH(pc.getLawful());
		String title = "";
		DailyTable dq = DailyTable.getInstance().loadDaily(pc);
		if (pc.isGm()) {
			title = "\\f3【\\f>極限GM\\f3】";
		}
		else {
			switch (dq.getMQStep()){
			case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11:
				title = "\\fD【新兵】";
				break;
			case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 21: case 22:
			case 23: case 24: case 25: case 26: case 27: case 28:
				title = "\\f2【勇士】";
				break;
			case 29: case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 38:
				title = "\\fU【A10】";
				break;
			case 39: case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48 :case 49:
			case 50: case 51: case 52: case 53: case 54:
				title = "\\f=【守護者】";
				break;
			case 55: case 56: case 57: case 101: case 102: case 103: case 104:
				title = "\\fD【英雄】";
				break;
			case 105: case 201: case 202: case 203: case 204:
				title = "\\fU【滅魔者】";
				break;
			case 205: case 301: case 302: case 303: case 304:
				title = "\\fY【屠龍者】";
				break;
			case 305: case 401: case 402: case 403: case 404:
				title = "\\f=【無雙】";
				break;
			case 405: case 501: case 502: case 503: case 504:
				title = "\\fU【無冕】";
				break;
			case 505: case 601: case 602: case 603: case 604:
				title = "\\fY【天境】";
				break;
			case 605: case 701: case 702: case 703: case 704:
				title = "\\fU【弒神者】";
				break;
			case 705: case 801: case 802: case 803: case 804:
				title = "\\f=【傳說】";
				break;
			case 805: case 901: case 902: case 903: case 904:
				title = "\\fU=【永恆】";
				break;
			case 905:	
				title = "\\fY=【極限】";
				break;
			}
		}
		writeS(pc.getName()+ title);
		writeS(pc.getTitle());
		writeC(status);
		writeD(pc.getClanid());
		writeS(pc.getClanname()); // クラン名
		writeS(null); // ペッホチング？
		writeC(0); // ？
		/*
		 * if(pc.is_isInParty()) // パーティー中 { writeC(100 * pc.get_currentHp() /
		 * pc.get_maxHp()); } else { writeC(0xFF); }
		 */

		writeC(0xFF);
		if (pc.hasSkillEffect(STATUS_THIRD_SPEED)) {
			writeC(0x08); // 3段加速
		} else {
			writeC(0);
		}
		writeC(0); // PC = 0, Mon = Lv
		writeC(0); // ？
		writeC(0xFF);
		writeC(0xFF);
	}

	@Override
	public byte[] getContent() {
		if (_byte == null) {
			_byte = getBytes();
		}
		return _byte;
	}

	@Override
	public String getType() {
		return S_OTHER_CHAR_PACKS;
	}

}